package spring.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import spring.dao.StudentService;
import spring.pojo.Student;

/**
 * @author CJunGod 1038804380@qq.com
 *
 */
public class StudentServiceSImpl implements StudentService{
	
	private JdbcTemplate jdbcTemplate;

	public List<Student> query() {
		final List<Student> students = new ArrayList<Student>();
		String sql = "select id,name,age from student";
		jdbcTemplate.query(sql, new RowCallbackHandler() {
				
			public void processRow(ResultSet resultSet) throws SQLException {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				
				Student student = new Student(id,name,age);
				
				students.add(student);
			}
			
		});
		return students;
	}

	public void save(Student student) {
		String sql = "insert into student(name,age) values (?,?)";
		jdbcTemplate.update(sql,new Object[] {
				student.getName(),student.getAge()
		});
	}

	/**
	 * @return the jdbcTemplate
	 */
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	/**
	 * @param jdbcTemplate the jdbcTemplate to set
	 */
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

}
